package runner;

import io.FastaReadWrite;
import io.MotifReadWrite;

import java.util.ArrayList;
import java.util.Vector;

import utils.SaveLoad;

import motiffinder.MotifFinder;

public class MotifRunner {
	
	public static String predictedMotifFilename = "predictedmotif.txt";
	public static String predictedSitesName = "predictedsites.txt";
	public static String statisticsFilename = "timestatistics.txt";
	
	public static void main(String [] args)
	{
		Vector<String> sequence = new Vector<String>();
		Vector<Integer> idx = new Vector<Integer>();
		Vector<String> motifString = new Vector<String>();
		Vector<Integer> motifLen = new Vector<Integer>();
		Vector<Integer> duplicates = new Vector<Integer>();

		System.out.println("Ready to read");
		FastaReadWrite.read(sequence, idx, motifString, motifLen, duplicates);
		System.out.println("Done reading");
		
		// time to iterate through the sequence
		int currSeqIdx = 0;
		do
		{
			System.out.println("Sequence size : " + sequence.size());
			int dupNum = duplicates.get(currSeqIdx);
			String [] seqArr = new String[dupNum];
			for (int i = currSeqIdx; currSeqIdx < sequence.size() && i < currSeqIdx + dupNum; i++)
			{
				seqArr[i - currSeqIdx] = sequence.get(i);
			}
			
			MotifFinder mf = new MotifFinder();
			mf.setSequences(seqArr, motifLen.get(currSeqIdx)); // just test one set.
			System.out.println("Ready to run!");
			long start = System.currentTimeMillis();
			mf.run(10000, 500, 2); // every 500 iterations, check windows of size 2, sample one
			long end = System.currentTimeMillis();
			System.out.println("Done!");
			
			// Save
			MotifReadWrite.saveFile(mf.getMotifMatrix(), motifLen.get(currSeqIdx), predictedMotifFilename);
			ArrayList<Integer> predictedSites = mf.getSites();
			for (Integer pos : predictedSites)
			{
				SaveLoad.saveFile(predictedSitesName, "" + pos + "\n");
			}
			SaveLoad.saveFile(statisticsFilename, "" + (end - start) + "\n");
			currSeqIdx += dupNum;
		} while (currSeqIdx < sequence.size());

	}
}
